import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../views/Login";
import Home from "../views/HomePage"
import dispatch from "../views/pharmacy/dispatch";
import withdrawal from "../views/pharmacy/withdrawal"
import manage from "../views/pharmacy/manage";
import HomePage from "../views/HomePage";
import vision from "../views/vision";
import details from "../views/pharmacy/details";
import newDrugs from "../views/pharmacy/newDrugs"


Vue.use(VueRouter)

const routes = [
    {path: '/', redirect: 'login'},
    {path: '/login', component: Login},
    {
        path: '/home', component: HomePage,
        children: [
            {
                path: 'pharmacy', component: vision,
                children: [
                    {path: 'dispatch', component: dispatch},
                    {path: 'withdrawal', component: withdrawal},
                    {path: 'manage', component: manage},
                    {path: 'details', component: details},
                    {path: 'newDrugs', component: newDrugs},
                ]
            },
        ]
    },

]


const router = new VueRouter({
    routes
})
/*router.beforeEach((to, from, next) => {
    if (to.path == "/Login") {
        next('/login');
    }
    console.log("from.path:" + from.path)
    console.log("to.path:" + to.path)
    console.log(localStorage.getItem("token"))
    // next()
    if (localStorage.getItem("token") == 11) {
        next();
    } else {
        next('/Login')
    }
})*/

router.beforeEach((to, from, next) => {
    // 先判断浏览器中是否已经有token了，有则true，否则false
    const isLogin = localStorage.token ? true : false

    //然后判断要去往的页面，如果是去往login和register页面的，就直接放行
    //如果不是去往login和register页面，则判断有没有token，如果有token就放行，否则就跳转login页面
    if (to.path == '/login') {
        next()
    } else {
        isLogin ? next() : next('/login')
    }

})

export default router
